package com.intelligentmeterreadingsys.report.activity;

import java.util.List;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.EngineeringOrderPoVo;
import com.intelligentmeterreadingsys.report.db.ReportDao;
import com.intelligentmeterreadingsys.util.SafetyExitUtil;
import com.intelligentmeterreadingsys.waterlist.copywater.activity.ImageSwitcherActivity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 查询工程单
 * 
 * @author king
 * 
 */
public class ReportQueryActivity extends Activity implements OnClickListener{

	
	public Button button3;// 视频
	public Button button4;// 声音
	public Button button5;// 图片

	public TextView textview1;// 名称
	public TextView textview2;// 类型
	public TextView textview3;// 描述
	
	public GestureDetector mGestureDetector;
	public List<EngineeringOrderPoVo> list = null;

	public int i = 0;// 当前页数
	
	private static final int dailogID=1;
	
	public SafetyExitUtil safe=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.orderinfo);
		init();
	}

	/**
	 * 初始化控件
	 */
	public void init() {
		
		button3 = (Button) findViewById(R.id.orderinfo_video);
		button4 = (Button) findViewById(R.id.orderinfo_sound);
		button5 = (Button) findViewById(R.id.orderinfo_img);
		textview1=(TextView)findViewById(R.id.orderinfo_name);
		textview2=(TextView)findViewById(R.id.orderinfo_type);
		textview3=(TextView)findViewById(R.id.orderinfo_desc);
		
		
		button3.setOnClickListener(this);
		button4.setOnClickListener(this);
		button5.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		if(list==null||list.size()<1){
			return;
		}
		EngineeringOrderPoVo eng=list.get(0);
         if(v.getId()==button3.getId()){
        	if(eng.getVideopath()!=null&&!eng.getVideopath().equals("")){
        		 if(list.get(0).getVideopath()!=null){
        			 Intent it = new Intent(Intent.ACTION_VIEW);
                     it.setDataAndType(Uri.parse("file://" + eng.getVideopath()), "video/avi");
                     startActivity(it); 
        		 }
        	}else{
        		Toast.makeText(ReportQueryActivity.this, "没有录视频", 1000).show();
        	}
        	   
         }else if(button4.getId()==v.getId()){
        	 if(eng.getSoundpath()!=null&&!eng.getSoundpath().equals("")){
        		 if(list.get(0).getVideopath()!=null){
        			 Intent it = new Intent(Intent.ACTION_VIEW);
                     it.setDataAndType(Uri.parse("file://" + eng.getSoundpath()), "audio/amr");
                     startActivity(it); 
        		 }
        	}else{
        		Toast.makeText(ReportQueryActivity.this, "没有录声音", 1000).show();
        		
        	}
        	       
         }else if(button5.getId()==v.getId()){
        	 if(list==null||list.size()<1){
        		 Toast.makeText(ReportQueryActivity.this, "没有工程单", Toast.LENGTH_LONG).show();
        		 return;
        	 }
        	
        	 Intent intent=new Intent(ReportQueryActivity.this, ImageSwitcherActivity.class);
        	 Bundle bundle=new Bundle();
        	 bundle.putString("status", "1");
        	 bundle.putString("otherid", list.get(i).getId()+"");
        	 intent.putExtras(bundle);
        	 
        	 startActivity(intent);
         }
	}

	/**
	 * 给界面添加数据
	 */
	public void addDate(){
		textview1.setText("");
		textview2.setText("");
		textview3.setText("");
		
		if(list!=null&&list.size()>0){
			EngineeringOrderPoVo order=list.get(i);
			textview1.setText(order.getName());
			textview2.setText(order.getTypenumber());
			textview3.setText(order.getDesc());
		}
		
	}
	/**
	 * 翻页
	 */
	public void filp(){
		if(i<0){
			i=0;
			Toast.makeText(ReportQueryActivity.this, "没有前面的数据了！", Toast.LENGTH_LONG).show();
		}else if(i>(list.size()-1)){
			i=list.size()-1;
			Toast.makeText(ReportQueryActivity.this, "没有后面的数据了！", Toast.LENGTH_LONG).show();
		}else{
			addDate();
		}
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		    
		return super.onCreateDialog(id);
	}
	@Override
	protected void onPause() {
	
		safe=new SafetyExitUtil(ReportQueryActivity.this);
		safe.start();
		
		super.onPause();
	}

	@Override
	protected void onResume() {
		OrderQueryMainActivity act=(OrderQueryMainActivity)getParent();
		
		ReportDao dao=new ReportDao(this);
		this.list=dao.queryOrder(act.getOrderid());
		dao.close();
		addDate();
	   if(safe!=null){
		   safe.stop();
	   }
		
		
		super.onResume();
	}
}
